package com.ohos.carbon.slice;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DirectionalLayout;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.ohos.carbon.slice.MainAbilitySlice.pf;

public class HistogramViewDemo extends AbilitySlice {

    private DirectionalLayout myLayout = new DirectionalLayout(this);

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        ComponentContainer.LayoutConfig config = new ComponentContainer.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_PARENT, ComponentContainer.LayoutConfig.MATCH_PARENT);
        myLayout.setLayoutConfig(config);
        HistogramView histogramView = new HistogramView(this);

        List<HistogramView.Histogram> list = new ArrayList<>();
        histogramView.setLayoutConfig(config);
        myLayout.addComponent(histogramView);
        super.setUIContent(myLayout);

        HistogramView.Histogram histogram1 = new HistogramView.Histogram(200);
        HistogramView.Histogram histogram2 = new HistogramView.Histogram(400);
        HistogramView.Histogram histogram3 = new HistogramView.Histogram(100);
        HistogramView.Histogram histogram4 = new HistogramView.Histogram(700);
        HistogramView.Histogram histogram5 = new HistogramView.Histogram(600);
        HistogramView.Histogram histogram6 = new HistogramView.Histogram(300);
        HistogramView.Histogram histogram7 = new HistogramView.Histogram(500);
        list.add(histogram1);
        list.add(histogram2);
        list.add(histogram3);
        list.add(histogram4);
        list.add(histogram5);
        list.add(histogram6);
        list.add(histogram7);
        histogramView.setData(list);

        MainAbilitySlice.all = (Map<String, Integer>) pf.getAll();
        if (MainAbilitySlice.all.size() != 0) {
            if (MainAbilitySlice.all.containsKey("Bar chart")) {
                for (Map.Entry<String, Integer> entry : MainAbilitySlice.all.entrySet()) {
                    if (entry.getKey().equals("Bar chart")) {
                        int integer = entry.getValue();
                        integer++;
                        pf.putInt("Bar chart", integer);
                        pf.flushSync();
                    }
                }
            } else {
                pf.putInt("Bar chart", 1);
                pf.flushSync();
            }
        } else {
            pf.putInt("Bar chart", 1);
            pf.flushSync();
        }
    }
}
